import java.sql.SQLOutput;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2024-04-06
 * Time: 18:20
 */
class StringUtil {
    public boolean judge(String s) {
        return s.startsWith("蔡") && s.length() == 3;
    }
}

public class Test3 {

    public static void main(String[] args) {
        // 创建集合并填入数据
        ArrayList<String> manList = new ArrayList<>();
        Collections.addAll(manList, "蔡坤坤", "叶厚贤,", "刘不甜", "吴倩", "蔡加", "蔡亮亮");

        // 获取所有以 “蔡” 开头，并且长度为 3 的姓名
        manList.stream().filter(new StringUtil()::judge).forEach(System.out::println);
    }

    public static void main3(String[] args) {
        List<String> lists = new ArrayList<>();
        Collections.addAll(lists, "1", "2", "3", "4", "5");
        // 静态方法引用示例：
        List<Integer> collect = lists.stream().map(Integer::parseInt).collect(Collectors.toList());
        System.out.println(collect);
    }

    public static void main2(String[] args) {
        List<Integer> lists = new ArrayList<>();
        lists.add(1);
        lists.add(1);
        lists.add(1);
        lists.add(2);
        lists.add(3);
        Map<Integer, List<Integer>> collect = lists.stream().collect(Collectors.groupingBy(x -> x));
        System.out.println(collect);
    }

    public static void main1(String[] args) {
        // 初始化数据
        Actor actor1 = new Actor("张三", 18);
        Actor actor2 = new Actor("李四", 20);
        Actor actor3 = new Actor("王五", 22);
        List<Actor> actors = new ArrayList<>();
        Collections.addAll(actors, actor1, actor2, actor3);
        // 按照姓名分组
        Map<String, List<Actor>> collect = actors.stream().collect(Collectors.groupingBy(actor -> actor.getName()));
        // 打印结果
        System.out.println(collect);

        // 将不同课程的学生进行分类
        Map<String, List<Actor>> groupByCourse = actors.stream().collect(Collectors.groupingBy(actor -> actor.getName()));
        System.out.println(groupByCourse
        );
        Map<String, List<Actor>> groupByCourse1 = actors.stream().collect(Collectors.groupingBy(actor -> actor.getName(), Collectors.toList()));
        System.out.println(groupByCourse1);

        // 上面的方法中容器类型和值类型都是默认指定的，容器类型为：HashMap，值类型为：ArrayList
        // 可以通过下面的方法自定义返回结果、值的类型
        Map<String, List<Actor>> groupByCourse2 = actors.stream()
                .collect(Collectors.groupingBy(actor -> actor.getName(),
                        () -> new HashMap<>(),
                        Collectors.toList()));
        System.out.println(groupByCourse2);
    }
}
